home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / demos / texobj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-29  |  6.3 KB  |  273 lines

  1. /* texobj.c */
  2.  
  3. /*
  4.  * Example of using the 1.1 texture object functions.
  5.  * Also, this demo utilizes Mesa's fast texture map path.
  6.  *
  7.  * Brian Paul   June 1996
  8.  */
  9.  
  10.  
  11.  
  12.  
  13. #include <math.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "gltk.h"
  17.  
  18.  
  19. static GLuint TexObj[2];
  20. static GLfloat Angle = 0.0f;
  21. static GLboolean HaveTexObj = GL_FALSE;
  22.  
  23.  
  24. #if defined(GL_VERSION_1_1_fo)
  25. #  define TEXTURE_OBJECT 1
  26. #elif defined(GL_EXT_texture_object)
  27. #  define TEXTURE_OBJECT 1
  28. #  define glBindTexture(A,B)     glBindTextureEXT(A,B)
  29. #  define glGenTextures(A,B)     glGenTexturesEXT(A,B)
  30. #  define glDeleteTextures(A,B)  glDeleteTexturesEXT(A,B)
  31. #endif
  32.  
  33.  
  34.  
  35.  
  36. static void draw( void )
  37. {
  38.    /*   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );*/
  39.    glClear( GL_COLOR_BUFFER_BIT );
  40.  
  41.    glColor3f( 1.0, 1.0, 1.0 );
  42.  
  43.    /* draw first polygon */
  44.    glPushMatrix();
  45.    glTranslatef( -1.0, 0.0, 0.0 );
  46.    glRotatef( Angle, 0.0, 0.0, 1.0 );
  47.    if (HaveTexObj) {
  48. #ifdef TEXTURE_OBJECT
  49.       glBindTexture( GL_TEXTURE_2D, TexObj[0] );
  50. #endif
  51.    }
  52.    else {
  53.       glCallList( TexObj[0] );
  54.    }
  55.    glBegin( GL_POLYGON );
  56.    glTexCoord2f( 0.0, 0.0 );   glVertex2f( -1.0, -1.0 );
  57.    glTexCoord2f( 1.0, 0.0 );   glVertex2f(  1.0, -1.0 );
  58.    glTexCoord2f( 1.0, 1.0 );   glVertex2f(  1.0,  1.0 );
  59.    glTexCoord2f( 0.0, 1.0 );   glVertex2f( -1.0,  1.0 );
  60.    glEnd();
  61.    glPopMatrix();
  62.  
  63.    /* draw second polygon */
  64.    glPushMatrix();
  65.    glTranslatef( 1.0, 0.0, 0.0 );
  66.    glRotatef( Angle-90.0, 0.0, 1.0, 0.0 );
  67.    if (HaveTexObj) {
  68. #ifdef TEXTURE_OBJECT
  69.       glBindTexture( GL_TEXTURE_2D, TexObj[1] );
  70. #endif
  71.    }
  72.    else {
  73.       glCallList( TexObj[0] );
  74.    }
  75.    glBegin( GL_POLYGON );
  76.    glTexCoord2f( 0.0, 0.0 );   glVertex2f( -1.0, -1.0 );
  77.    glTexCoord2f( 1.0, 0.0 );   glVertex2f(  1.0, -1.0 );
  78.    glTexCoord2f( 1.0, 1.0 );   glVertex2f(  1.0,  1.0 );
  79.    glTexCoord2f( 0.0, 1.0 );   glVertex2f( -1.0,  1.0 );
  80.    glEnd();
  81.    glPopMatrix();
  82.  
  83.    tkSwapBuffers();
  84. }
  85.  
  86.  
  87.  
  88. static void idle( void )
  89. {
  90.    Angle += 2.0;
  91.    draw();
  92. }
  93.  
  94.  
  95.  
  96. /* change view Angle, exit upon ESC */
  97. static GLenum key(int k, GLenum mask)
  98. {
  99.    switch (k) {
  100.       case TK_ESCAPE:
  101. #ifdef TEXTURE_OBJECT
  102.          glDeleteTextures( 2, TexObj );
  103. #endif
  104.      tkQuit();
  105.    }
  106.    return GL_FALSE;
  107. }
  108.  
  109.  
  110.  
  111. /* new window size or exposure */
  112. static void reshape( int width, int height )
  113. {
  114.    glViewport(0, 0, (GLint)width, (GLint)height);
  115.    glMatrixMode(GL_PROJECTION);
  116.    glLoadIdentity();
  117.    /*   glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
  118.    glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
  119.    glMatrixMode(GL_MODELVIEW);
  120.    glLoadIdentity();
  121.    glTranslatef( 0.0, 0.0, -8.0 );
  122. }
  123.  
  124.  
  125. static void init( void )
  126. {
  127.    static int width=8, height=8;
  128.    static GLubyte tex1[] = {
  129.      0, 0, 0, 0, 0, 0, 0, 0,
  130.      0, 0, 0, 0, 1, 0, 0, 0,
  131.      0, 0, 0, 1, 1, 0, 0, 0,
  132.      0, 0, 0, 0, 1, 0, 0, 0,
  133.      0, 0, 0, 0, 1, 0, 0, 0,
  134.      0, 0, 0, 0, 1, 0, 0, 0,
  135.      0, 0, 0, 1, 1, 1, 0, 0,
  136.      0, 0, 0, 0, 0, 0, 0, 0 };
  137.  
  138.    static GLubyte tex2[] = {
  139.      0, 0, 0, 0, 0, 0, 0, 0,
  140.      0, 0, 0, 2, 2, 0, 0, 0,
  141.      0, 0, 2, 0, 0, 2, 0, 0,
  142.      0, 0, 0, 0, 0, 2, 0, 0,
  143.      0, 0, 0, 0, 2, 0, 0, 0,
  144.      0, 0, 0, 2, 0, 0, 0, 0,
  145.      0, 0, 2, 2, 2, 2, 0, 0,
  146.      0, 0, 0, 0, 0, 0, 0, 0 };
  147.  
  148.    GLubyte tex[64][3];
  149.    GLint i, j;
  150.  
  151.  
  152.    glDisable( GL_DITHER );
  153.  
  154.    /* Setup texturing */
  155.    glEnable( GL_TEXTURE_2D );
  156.    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
  157.    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
  158.  
  159.  
  160.    /* generate texture object IDs */
  161.    if (HaveTexObj) {
  162. #ifdef TEXTURE_OBJECT
  163.       glGenTextures( 2, TexObj );
  164. #endif
  165.    }
  166.    else {
  167.       TexObj[0] = glGenLists(2);
  168.       TexObj[1] = TexObj[0]+1;
  169.    }
  170.  
  171.    /* setup first texture object */
  172.    if (HaveTexObj) {
  173. #ifdef TEXTURE_OBJECT
  174.       glBindTexture( GL_TEXTURE_2D, TexObj[0] );
  175. #endif
  176.    }
  177.    else {
  178.       glNewList( TexObj[0], GL_COMPILE );
  179.    }
  180.    /* red on white */
  181.    for (i=0;i<height;i++) {
  182.       for (j=0;j<width;j++) {
  183.          int p = i*width+j;
  184.          if (tex1[(height-i-1)*width+j]) {
  185.             tex[p][0] = 255;   tex[p][1] = 0;     tex[p][2] = 0;
  186.          }
  187.          else {
  188.             tex[p][0] = 255;   tex[p][1] = 255;   tex[p][2] = 255;
  189.          }
  190.       }
  191.    }
  192.  
  193.    glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
  194.                  GL_RGB, GL_UNSIGNED_BYTE, tex );
  195.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  196.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  197.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  198.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  199.    if (!HaveTexObj) {
  200.       glEndList();
  201.    }
  202.    /* end of texture object */
  203.  
  204.    /* setup second texture object */
  205.    if (HaveTexObj) {
  206. #ifdef TEXTURE_OBJECT
  207.       glBindTexture( GL_TEXTURE_2D, TexObj[1] );
  208. #endif
  209.    }
  210.    else {
  211.       glNewList( TexObj[1], GL_COMPILE );
  212.    }
  213.    /* green on blue */
  214.    for (i=0;i<height;i++) {
  215.       for (j=0;j<width;j++) {
  216.          int p = i*width+j;
  217.          if (tex2[(height-i-1)*width+j]) {
  218.             tex[p][0] = 0;     tex[p][1] = 255;   tex[p][2] = 0;
  219.          }
  220.          else {
  221.             tex[p][0] = 0;     tex[p][1] = 0;     tex[p][2] = 255;
  222.          }
  223.       }
  224.    }
  225.    glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
  226.                  GL_RGB, GL_UNSIGNED_BYTE, tex );
  227.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  228.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  229.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  230.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  231.    if (!HaveTexObj) {
  232.       glEndList();
  233.    }
  234.    /* end texture object */
  235.  
  236. }
  237.  
  238.  
  239.  
  240. int main( int argc, char *argv[] )
  241. {
  242.    tkInitPosition(0, 0, 300, 300);
  243.    tkInitDisplayMode( TK_RGB | TK_DEPTH | TK_DOUBLE | TK_DIRECT );
  244.  
  245.    if (tkInitWindow("Texture Objects") == GL_FALSE) {
  246.       tkQuit();
  247.    }
  248.  
  249.    /* check that renderer has the GL_EXT_texture_object extension
  250.     * or supports OpenGL 1.1
  251.     */
  252. #ifdef TEXTURE_OBJECT
  253.    {
  254.       char *exten = (char *) glGetString( GL_EXTENSIONS );
  255.       char *version = (char *) glGetString( GL_VERSION );
  256.       if (   strstr( exten, "GL_EXT_texture_object" )
  257.           || strncmp( version, "1.1", 3 )==0 ) {
  258.          HaveTexObj = GL_TRUE;
  259.       }
  260.    }
  261. #endif
  262.  
  263.    init();
  264.  
  265.    tkExposeFunc( reshape );
  266.    tkReshapeFunc( reshape );
  267.    tkKeyDownFunc( key );
  268.    tkIdleFunc( idle );
  269.    tkDisplayFunc( draw );
  270.    tkExec();
  271.    return 0;
  272. }
  273.